package experiments.lm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;

public class ParallelLM {

	public static void main(String[] args) {
		ParallelLM lm = new ParallelLM();
		List<String> langs = lm.loadISOLanguageList();
		List<String[]> pairs = generateUniquePermutations(langs);
		for (String[] e : pairs) {
			System.out.println(e[0] + "-" + e[1]);
		}
		//get all lang pairs
		//for each metaphor, look it up in the lm, and look at a dictionary overlap in the nearest terms. or cosine 
		
	}

	public List<Language> loadLanguageList() {
		List<Language> langs = new ArrayList<Language>();
		try {
			List<String> lines = IOUtils.readLines(Thread.currentThread().getContextClassLoader().getResourceAsStream("lang/iso-codes"));
			Pattern linePatern = Pattern.compile("<.*>(.*) (\\(.*\\))<.*>");
			for (String line : lines) {
				Matcher m = linePatern.matcher(line);
				while (m.find()) {
					Language l = new Language();
					String iso = m.group(1);

					if (iso.contains("_")) {
						String[] parts = iso.split("_");
						if (parts.length == 2) {
							iso = parts[0];
							l.setDialect(parts[1]);
						}
					}

					if (iso.length() == 2) {
						l.setIso6391(iso);
					} else if (iso.length() == 3) {
						l.setIso6392(iso);
					}

					String name = m.group(2).replaceAll("\\)", "").replaceAll("\\(", "");
					l.setFullName(name);
					langs.add(l);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return langs;
	}

	public List<String> loadISOLanguageList() {
		List<String> langs = new ArrayList<String>();
		try {
			List<String> lines = IOUtils.readLines(Thread.currentThread().getContextClassLoader().getResourceAsStream("lang/iso-codes"));
			Pattern linePatern = Pattern.compile("<.*>(.*) (\\(.*\\))<.*>");
			for (String line : lines) {
				Matcher m = linePatern.matcher(line);
				while (m.find()) {
					Language l = new Language();
					String iso = m.group(1);
					langs.add(iso);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return langs;
	}



	public static List<String[]> generateUniquePermutations(List<String> languages) {
		List<String[]> languagePairs = new ArrayList<String[]>();

		int maxIndex = languages.size();
		for (int currentIndex = 0; currentIndex < maxIndex; currentIndex++) {
			String currentLanguage = languages.get(currentIndex);
			for (int tempIndex = currentIndex + 1; tempIndex < maxIndex; tempIndex++) {
				String[] pair = new String[2];
				pair[0] = currentLanguage;
				pair[1] = languages.get(tempIndex);
				languagePairs.add(pair);
			}
		}

		return languagePairs;
	}
}
